/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DB;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Mathias
 */
@Entity
@Table(name = "StatusMessage")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "StatusMessage.findAll", query = "SELECT s FROM StatusMessage s"),
    @NamedQuery(name = "StatusMessage.findById", query = "SELECT s FROM StatusMessage s WHERE s.id = :id"),
    @NamedQuery(name = "StatusMessage.findByText", query = "SELECT s FROM StatusMessage s WHERE s.text = :text"),
    @NamedQuery(name = "StatusMessage.findByTmeposted", query = "SELECT s FROM StatusMessage s WHERE s.tmeposted = :tmeposted")})
public class StatusMessage implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    
    @Column(name = "text")
    private String text;
    
    @Basic(optional = false)
    @Column(name = "tmeposted")
    @Temporal(TemporalType.TIMESTAMP)
    private Date tmeposted;
    
    @JoinColumn(name = "sender_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User senderId;

    public StatusMessage() {
    }

    public StatusMessage(Integer id) {
        this.id = id;
    }

    public StatusMessage(Integer id, Date tmeposted) {
        this.id = id;
        this.tmeposted = tmeposted;
    }

    public StatusMessage(String text, Date tmeposted, User senderId) {
        this.text = text;
        this.tmeposted = tmeposted;
        this.senderId = senderId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Date getTmeposted() {
        return tmeposted;
    }

    public void setTmeposted(Date tmeposted) {
        this.tmeposted = tmeposted;
    }

    public User getSenderId() {
        return senderId;
    }

    public void setSenderId(User senderId) {
        this.senderId = senderId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof StatusMessage)) {
            return false;
        }
        StatusMessage other = (StatusMessage) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "DB.StatusMessage[ id=" + id + " ]";
    }
    
    
    
    public static class StatusComparator implements Comparator<StatusMessage>{

        @Override
        public int compare(StatusMessage x, StatusMessage y) {
            
            if(x == null && y == null)
            {
                return 0;
            } else if(x == null)
            {
                return -1;
            } else if(y == null)
            {
                return 1;
            }
            else if(x.getId() > y.getId()){
                return -1;
            }
            else if(x.getId() < y.getId())
            {
                return 1;
            }
            else if(x.getId() == y.getId())
            {
                return 0;
            }
            return 0;

        }
        
    }
    
}
